﻿using Ecommerce.Areas.Manager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ecommerce.Areas.Home.Models;
using Ecommerce.Common;

namespace Ecommerce.Areas.Home.Controllers
{
    public class TemplateController : Controller
    {

        private DatabaseContext db = new DatabaseContext();

        //
        // GET: /Home/Home/

        public ActionResult Index(string cateID)
        {
            int ID = 0;
            try
            {
                ID = Int32.Parse(cateID);
            }
            catch { }

            List<Product> model = new List<Product>();

            if (ID != 0)
            {
                model = db.Products
                    .Where(x => x.ProductStatus != (int)EcommerceEnum.ProductStatus.Deactive
                             && x.ProductStatus != (int)EcommerceEnum.ProductStatus.FreeProduct)
                    .Where(x => x.CategoryId == ID)
                    .OrderBy(x => x.ProductStatus).ToList();
            }
            else
            {
                model = db.Products
                    .Where(x => x.ProductStatus != (int)EcommerceEnum.ProductStatus.Deactive
                                && x.ProductStatus != (int)EcommerceEnum.ProductStatus.FreeProduct)
                    .OrderBy(x => x.ProductStatus).ToList();
            }

            foreach (var item in model)
            {
                string[] imgList = item.Image.Split('|');
                item.ImgThum = imgList[0];
            }

            return View(model);
        }

        public ActionResult Detail(string prodID)
        {

            int ID = 0;
            try
            {
                ID = Int32.Parse(prodID);
            }
            catch
            {
                return RedirectToAction("Index");
            }

            HomeModel p = new HomeModel();

            p.Product = db.Products.Find(ID);
            if (p == null)
            {
                return RedirectToAction("Index");
            }

            List<string> ImageList = p.Product.Image.Split('|').ToList();
            p.Product.ImgThum = ImageList[0];
            ImageList.RemoveAt(0);
            ImageList.RemoveAt(ImageList.Count - 1);
            p.ImageList = ImageList;

            ViewBag.Title = p.Product.ProdName;

            return View(p);
        }

        public ActionResult FreeDownload(string cateID)
        {
            int ID = 0;
            try
            {
                ID = Int32.Parse(cateID);
            }
            catch { }

            List<Product> model = new List<Product>();

            if (ID != 0)
            {
                model = db.Products
                    .Where(x => x.ProductStatus != (int)EcommerceEnum.ProductStatus.FreeProduct)
                    .Where(x => x.CategoryId == ID)
                    .OrderBy(x => x.ProductStatus).ToList();
            }
            else
            {
                model = db.Products
                    .Where(x => x.ProductStatus == (int)EcommerceEnum.ProductStatus.FreeProduct)
                    .OrderBy(x => x.ProductStatus).ToList();
            }

            foreach (var item in model)
            {
                string[] imgList = item.Image.Split('|');
                item.ImgThum = imgList[0];
            }


            return View("Index", model);
        }

        [ChildActionOnly]
        public ActionResult Categories()
        {
            var model = db.Categories.OrderByDescending(x => x.Order).ToList();
            return PartialView("_Categories", model);
        }

    }
}
